package com.lykj.lib_base.component

import android.content.Context
import android.util.Log
import com.bumptech.glide.Glide
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.Registry
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool
import com.bumptech.glide.load.engine.cache.DiskLruCacheWrapper
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory
import com.bumptech.glide.load.engine.cache.LruResourceCache
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.module.AppGlideModule
import com.bumptech.glide.request.RequestOptions
import com.lykj.lib_base.component.glide.OkHttpLoader
import com.lykj.lib_base.net.http.OkHttpManager
import java.io.File
import java.io.InputStream

/**
 * ==========================
 * 自定义的Glide应用设置
 * ==========================
 *
 * @author gosha
 * @date 2022/9/2-14:04
 */
@GlideModule
class AppGlideComponent :AppGlideModule(){
    override fun applyOptions(context: Context, builder: GlideBuilder) {
        //内存缓存
        //覆写内存缓存大小
        val memoryCacheSizeBytes = 1024 * 1024 * 500 // 50mb
//        builder.setMemoryCache(LruResourceCache(memoryCacheSizeBytes.toLong()))

        // 读写外部缓存目录不需要申请存储权限
        val diskCacheFile = File(context.cacheDir, "glide")
        // 如果这个路径是一个文件
        if (diskCacheFile.exists() && diskCacheFile.isFile) {
            // 执行删除操作
            diskCacheFile.delete()
        }
        // 如果这个路径不存在
        if (!diskCacheFile.exists()) {
            // 创建多级目录
            diskCacheFile.mkdirs()
        }
        builder.setDiskCache {
            DiskLruCacheWrapper.create(diskCacheFile, memoryCacheSizeBytes.toLong())
        }
        val calculator: MemorySizeCalculator = MemorySizeCalculator.Builder(context).build()
        val defaultMemoryCacheSize: Int = calculator.memoryCacheSize
        val defaultBitmapPoolSize: Int = calculator.bitmapPoolSize
        val customMemoryCacheSize: Long = (1.2 * defaultMemoryCacheSize).toLong()
        val customBitmapPoolSize: Long = (1.2 * defaultBitmapPoolSize).toLong()
        builder.setMemoryCache(LruResourceCache(customMemoryCacheSize))
        builder.setBitmapPool(LruBitmapPool(customBitmapPoolSize))

        //磁盘缓存
        val diskCacheSizeBytes = 1024 * 1024 * 100L// 100 MB
        builder.setDiskCache(
            InternalCacheDiskCacheFactory(
                context,
                "GlideDiskCache",
                diskCacheSizeBytes
            )
        )

        //设置默认请求选项
        builder.setDefaultRequestOptions(
            RequestOptions()
                .format(DecodeFormat.DEFAULT)
        )

        //日志级别
        builder.setLogLevel(Log.ERROR)
    }

    override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
        // Glide 默认使用的是 HttpURLConnection 来做网络请求，这里切换成更高效的 OkHttp
        registry.replace(GlideUrl::class.java, InputStream::class.java, OkHttpLoader.Factory(
            OkHttpManager.getDefaultOkHttpClient()))
    }

    override fun isManifestParsingEnabled(): Boolean {
        return false
    }
}